home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Arsenal Files 4
/
The Arsenal Files 4 (Arsenal Computer).ISO
/
casm
/
au116-as.exe
/
IBMDOS
/
HANDLE.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1994-10-24
|
3KB
|
155 lines
#include "..\au.hpp"
#include <fcntl.h>
#include <sys\stat.h>
#include <errno.h>
/*****************************************************************************/
/* Replace C's open */
STATUS HANDLE::open(AU *au, char *filename, int access)
{
char string[200];
if (access & O_CREAT)
fh = ::open(filename, access, S_IWRITE|S_IREAD);
else
fh = ::open(filename, access);
if (fh == -1)
{
if (_doserrno == ENOENT) /* file simply missing */
return FAILURE;
sprintf(string, "Error opening '%s',", filename);
if (_doserrno == ENOPATH)
{
au_printf_error(au, "%s %s", string, "Illegal Path");
return FAILURE;
}
else if (_doserrno == EMFILE)
au_printf_error(au, "%s %s", string, "Too Many Files Open");
else if (_doserrno == EACCES)
au_printf_error(au, "%s %s", string, "Permission Denied");
return FAILURE;
}
reset();
return SUCCESS;
}
/*******************************************************************/
void HANDLE::close()
{
if (fh >= 0)
::close(fh);
fh = -1;
}
/*******************************************************************/
long HANDLE::seek(long pos, int from)
{
long logical_pos, new_pos;
if (bufferPointer >= bufferEnd)
return lseek(fh, pos, from);
logical_pos = bufferFilePos + bufferPointer;
if (from == SEEK_END)
{
#if 0
/* Work on optimizing this a little later */
reset();
return lseek(fh, pos, from);
#endif
new_pos = lseek(fh, pos, from);
if (new_pos >= bufferFilePos && new_pos <= bufferFilePos + bufferEnd)
{
bufferPointer = new_pos - bufferFilePos;
return new_pos;
}
else
{
long pos;
pos = new_pos-readBufferSize/2;
if (pos < 0)
pos = 0;
reset();
lseek(fh, pos, SEEK_SET);
read_char(); // force it to load in the buffer
if (new_pos >= bufferFilePos && new_pos <= bufferFilePos + bufferEnd)
{
bufferPointer = new_pos - bufferFilePos;
return new_pos;
}
else
return -1;
}
}
else
{
if (from == SEEK_CUR)
new_pos = logical_pos + pos;
else
new_pos = pos;
if (new_pos >= bufferFilePos && new_pos <= bufferFilePos + bufferEnd)
{
bufferPointer = new_pos - bufferFilePos;
return new_pos;
}
else
{
reset();
return lseek(fh, new_pos, SEEK_SET);
}
}
}
/*******************************************************************/
/* Peel one character off a HANDLE */
int HANDLE::read_char()
{
if (bufferPointer >= bufferEnd)
{
bufferFilePos = tell(fh);
bufferPointer=0;
bufferEnd = ::read(fh, buffer, readBufferSize);
if (bufferEnd==0)
return EOF;
}
return (unsigned int)buffer[bufferPointer++];
}
/********************************************************************/
void HANDLE::write_text(char *string)
{
char string2[200];
int p1=0,p2=0;
int len = strlen(string);
if (!is_open())
return;
if (bufferPointer < bufferEnd)
{
lseek(fh, bufferFilePos + bufferPointer, SEEK_SET);
}
while (p1<len)
{
if (string[p1] == '\n' && p1 > 0 && string[p1-1] != '\r')
string2[p2++] = '\r';
string2[p2++] = string[p1];
p1++;
}
string2[p2] = '\0';
::write(fh, string2, p2);
reset();
return;
}